package com.itheima.controller;

import com.itheima.domain.Book;
import com.itheima.service.BookService;
import com.itheima.service.BorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class BookController {
    @Autowired
    private BookService bookService;
    @Autowired
    private BorrowService borrowService;

    @RequestMapping("/book")
    public ModelAndView findBookById(Integer id) {
        Book book = bookService.findBookById(id);
        ModelAndView modelAndView = new ModelAndView("views/book.jsp");
//        modelAndView.setViewName("views/book.jsp");
        modelAndView.addObject("book", book);
        return modelAndView;
    }
    @GetMapping
    public String index() {
        return "index.jsp";
    }
    @PostMapping("addBookServlet")
    public ModelAndView addbookServlet( @RequestParam("bookName") String bookName,@RequestParam("author") String author,
                                        @RequestParam("price") String price,@RequestParam("file") MultipartFile file) {
        Book book = new Book(null, bookName, author, price, null);
        System.out.println("新增的book名字="+bookName);
        if (file == null || file.isEmpty()) {
            ModelAndView modelAndView = new ModelAndView("addbook.jsp");
            modelAndView.addObject("msg", "未选择文件或文件为空！");
            return modelAndView;
        }
        try {
            book.setFile(file.getBytes());
        } catch (IOException e) {
            throw new RuntimeException("文件上传出错："+e);
        }
        ModelAndView modelAndView = new ModelAndView("addbook");

        if( bookService.addBook(book) == false){
            modelAndView.addObject("msg", "添加图书失败："+bookName);
            return modelAndView;
        }
        modelAndView.addObject("msg", "添加图书成功："+bookName);
        return modelAndView;
    }
    @GetMapping("allBook")
    public ModelAndView allBook() {
        ModelAndView modelAndView = new ModelAndView("allBook");
        modelAndView.addObject("bookList", bookService.findAllBook());
        return modelAndView;
    }
    @PostMapping("update/book")
    public ModelAndView changeBook(@RequestParam Integer id,@RequestParam String bookName,@RequestParam String author,@RequestParam String price) {
        System.out.println("修改的名称="+bookName);
        Book book = new Book(id, bookName, author, price, null);
        ModelAndView modelAndView = new ModelAndView("changebook");
        modelAndView.addObject("book", book);
        if(bookService.changeBook(book)){
            modelAndView.addObject("msg", "修改图书成功："+book.getBookName());
            return modelAndView;
        }
        modelAndView.addObject("msg", "修改图书失败！");
        return modelAndView;
    }
    @PostMapping("delete/book")
    public ModelAndView deleteBook(@RequestParam Integer id) {
        ModelAndView modelAndView = new ModelAndView("deletebook");
        if(bookService.deleteBook(id)){
            modelAndView.addObject("msg", "删除id为"+id+"的图书成功！");
            return modelAndView;
        }
        modelAndView.addObject("msg", "删除id为"+id+"的图书失败！");
        return modelAndView;
    }
    @PostMapping("search/book")
    public ModelAndView searchBook(@RequestParam String bookName,@RequestParam String author,@RequestParam String minPrice,@RequestParam String maxPrice) {
        System.out.println("查询的图书名称="+bookName);
        ModelAndView modelAndView = new ModelAndView("findbook");
        modelAndView.addObject("msg", "查找成功！");
        modelAndView.addObject("bookList", bookService.findBookByNameAndAuthorAndPrice(bookName, author, minPrice, maxPrice));

        return modelAndView;

    }
    @PostMapping("borrow")
    public ModelAndView  borrowBook(@RequestParam Integer id, @RequestParam Integer userId) {
            ModelAndView modelAndView = new ModelAndView("allBook");
        if (borrowService.findByBook(id)){
            modelAndView.addObject("msg", "借阅失败！");
            return modelAndView;
        }
        boolean b = borrowService.addBorrow(id, userId);
        if ( b){
            modelAndView.addObject("msg", "借阅成功！");
        }else {
            modelAndView.addObject("msg", "借阅失败！");
        }
        return modelAndView;
    }



}

